local xing = fk.CreateSkill{
    name = "rmt__xing",
}
Fk:loadTranslationTable{
    ["rmt__xing"] = "形",
    [":rmt__xing"] = "每回合每项限一次，你可以撕毁持有的一个武将的足部/腿部/胸部/头部/全部，视为使用一张【南蛮入侵】/【水淹七军】/【桃】/【美人计】/结算四次的一张上述牌。",
    ["#rmt__xing-remove"] = "形：请撕毁一个武将的 %arg",
    ["#rmt__xing-all"] = "形：你可以撕毁%arg的全部，另即将使用的%arg2结算4次",
    ["#rmt__xing-remove-log"] = "%from 发动〖%arg〗撕毁了 %arg2 的 <font color='#791cc0'>%arg3</font>",
    ["#rmt__xing-all-log"] = "%from 发动〖%arg〗撕毁了 %arg2 的 <font color='#791cc0'>全部碎片</font>",
}

local function hasFragment(player,str)
  local mark = player:getTableMark("@[rmt__xing]")
  if #mark == 0 then return false end
  for index, value in ipairs(mark) do
      if table.contains(value[2],str) then
          return true
      end
  end
end

local function hasAllFragment(player)
  local mark = player:getTableMark("@[rmt__xing]")
  if #mark == 0 then return false end
  for index, value in ipairs(mark) do
      if #value[2] >= 4 then
          return true
      end
  end
end

xing:addEffect("viewas",{
    pattern = "savage_assault,drowning,peach,honey_trap",
    interaction = function (self, player)
        local names, all_names = {}, {"honey_trap","peach","drowning","savage_assault"}
        local marks = {"#rmt__huo1","#rmt__huo2","#rmt__huo3","#rmt__huo4"}
        for index, name in ipairs(all_names) do
            if hasFragment(player,marks[index]) or hasAllFragment(player) then
                table.insertIfNeed(names, name)
            end
        end
        names = player:getViewAsCardNames(xing.name, names, nil, player:getTableMark("rmt__xing-turn"))
        if #names == 0 then return end
        return UI.CardNameBox { choices = names, all_choices = all_names }
    end,
    view_as = function (self, cards, player)
        if not self.interaction.data then return end
        local card = Fk:cloneCard(self.interaction.data)
        card.skillName = self.name
        return card
    end,
    before_use = function (self, player, use)
        local room = player.room
        local all_names = {["honey_trap"]="#rmt__huo1",["peach"]="#rmt__huo2",["drowning"]="#rmt__huo3",["savage_assault"]="#rmt__huo4"}
        local mark = player:getTableMark("@[rmt__xing]")
        local choices, trueChoices = {}, {}
        for _, value in ipairs(mark) do
            if #value[2] >= 4 or table.contains(value[2],all_names[use.card.name]) then
                trueChoices[Fk:translate(value[1],"zh_CN").."("..tostring(#value[2])..")"] = value[1]
                table.insert(choices,Fk:translate(value[1],"zh_CN").."("..tostring(#value[2])..")")
            end
        end
        if #choices > 0 then
            local choice = room:askToChoice(player, {
                skill_name = xing.name,
                choices = choices,
                prompt = "#rmt__xing-remove:::"..all_names[use.card.name],
            })
            local trueChoice = trueChoices[choice]
            local removeIndex = {}
            for index, value in ipairs(mark) do
                if value[1] == trueChoice then
                    if #value[2] >= 4 then
                        if room:askToSkillInvoke(player,{
                            skill_name = xing.name,
                            prompt = "#rmt__xing-all:::"..value[1]..":"..use.card.name
                        }) then
                            value[2] = {}
                            table.insert(removeIndex,index)
                            use.additionalEffect = ( use.additionalEffect or 0 ) + 3
                            room:sendLog{
                                type = "#rmt__xing-all-log",
                                from = player.id,
                                arg = self.name,
                                arg2 = trueChoice,
                                toast = true,
                            }
                            break
                        end
                    end
                    room:sendLog{
                        type = "#rmt__xing-remove-log",
                        from = player.id,
                        arg = self.name,
                        arg2 = trueChoice,
                        arg3 = all_names[use.card.name],
                        toast = true,
                    }
                    table.removeOne(value[2],all_names[use.card.name])
                end
                if #value[2] == 0 then
                    table.insert(removeIndex,index)
                end
            end
            for _, index in ipairs(removeIndex) do
                table.remove(mark,index)
            end
            if #mark > 0 then
                room:setPlayerMark(player,"@[rmt__xing]",mark)
            else
                room:setPlayerMark(player,"@[rmt__xing]",0)
            end
        else
            return ""
        end
    end,
    after_use = function (self, player, use)
        local room = player.room
        local used = player:getTableMark("rmt__xing-turn")
        table.insert(used,use.card.name)
        room:setPlayerMark(player,"rmt__xing-turn",used)
    end,
    enabled_at_response = function (self, player, response)
        return not response
    end,
})

return xing